<?php
/**
 * Created by PhpStorm.
 * User: xudongsheng
 * Date: 2017/9/19
 * Time: 下午2:36
 */

namespace app\admin\controller;


use app\admin\model\AdItem;
use app\admin\model\AdPosition;
use think\Request;

class Ad extends Common
{
    /** 广告位 列表
     * @return mixed
     */
    public function adLists(){
         $data = AdPosition::with('adItemNum')->paginate(15);
         $this->assign('AdPosition',$data );
         return $this->fetch();
    }

    /** 创建  广告位
     * @param Request $request
     * @return mixed
     */
    public function createAdPosition(Request $request){
        $request->isPost() AND $this->createSave();
        $this->assign('type',AdPosition::$types);
        return $this->fetch();
    }

    /**
     * 创建广告位 之 保存
     */
    private function createSave(){
        // 缺少 表单 验证

        if (AdPosition::create(input('post.'),true)){
            $this->success('添加成功！',url('ad/adLists'));
        }
        $this->error('添加失败！');
    }

    /** 广告位 之 编辑
     * @param Request $request
     * @param $ad_position_id
     * @return mixed
     */
    public function editAdPosition(Request $request,$ad_position_id){
        ($ad_position = AdPosition::get($ad_position_id)) OR $this->error('此广告位不存在！');
        $request->isPost() AND $this->updatePosition($ad_position);
        $this->assign('type',AdPosition::$types);
        $this->assign('ad_position',$ad_position);
        return $this->fetch();
    }

    /** 广告位 之 编辑更新 保存
     * @param $ad_position
     */
    private function updatePosition($ad_position){
        // 缺少 表单 验证

        if ($ad_position->allowField(true)->isUpdate()->save(input('post.'))){
            $this->success('更新成功！',url('ad/adLists'));
        }
           $this->error('更新失败！');
    }

    /** 广告位 之 删除
     * @param $ad_position_id
     */
    public function deleteAdPosition($ad_position_id){
        ($ad_position = AdPosition::get($ad_position_id)) OR $this->error('此广告位不存在！');
        // 关联 删除......
        if ($ad_position->together('adItem')->delete()){
            $this->success('已删除！',url('ad/adLists'));
        }
        $this->error('删除失败！');
    }

    /** 广告 列表
     * @param $ad_position_id
     * @return mixed
     */
    public function aditem($ad_position_id){
          ($ad_position = AdPosition::get($ad_position_id)) OR $this->error('此广告位不存在！');
          $adItem = AdItem::with('adPosition')->where('ad_position_id',$ad_position_id)
                  ->order('sort','desc')
                  ->field('ad_item_id,title,start_time,end_time,ad_position_id,sort,status')
                  ->paginate(15);
          $this->assign('ad_position',$ad_position);
          $this->assign('adItem',$adItem);
          return $this->fetch();
    }

    /** 广告 排序
     * @param $ad_item_id
     */
    public function adItemSort($sort){
        (!is_array($sort) OR empty($sort)) AND $this->error('没有要排序项！');
        foreach ($sort as $k=>$v){
            AdItem::update([
                'ad_item_id'  =>  (int)$k,
                'sort'       =>  (int)$v
            ]);
        }
        $this->success('已排序~');
    }

    /** 删除 广告
     * @param $ad_item_id
     */
    public function deleteAdItem($ad_item_id){
        ($ad_item = AdItem::get($ad_item_id)) OR $this->error('此广告不存在！');
        if ($ad_item->delete()){
            $this->success('已删除！',url('ad/aditem',['ad_position_id'=>$ad_item->ad_position_id]));
        }
        $this->error('删除失败！');
    }

    public function createAdItem(Request $request,$ad_position_id){
        ($ad_position = AdPosition::get($ad_position_id)) OR $this->error('此广告位不存在！');
        $request->isPost() AND $this->createAdItemSave($ad_position);
        $this->assign('type',AdPosition::adItemType($ad_position->type));
        $this->assign('ad_position',$ad_position);
        $this->assign('start_time',date('Y-m-d'));
        $this->assign('end_time',date('Y-m-d',strtotime('+1 month')));
        return $this->fetch();
    }

    private function createAdItemSave($ad_position){
        // 表单 验证


        $data   =   input('post.');
        $data['start_time']     =   empty($data['start_time'])?time():strtotime($data['start_time']);
        $data['end_time']       =   empty($data['end_time'])?strtotime('+1 month'):strtotime($data['end_time']);
        $data['ad_position_id'] =   $ad_position->ad_position_id;
        if (AdItem::create($data,true)){
            $this->success('添加成功！',url('ad/aditem',['ad_position_id'=>$ad_position->ad_position_id]));
        }
        $this->error('添加失败！');
    }

    public function editAdItem(Request $request,$ad_item_id){
        ($ad_item = AdItem::get($ad_item_id)) OR $this->error('此广告不存在！');
        $request->isPost() AND $this->updateAdItem($ad_item);
        $ad_position = AdPosition::get($ad_item->ad_position_id);
        $this->assign('type',AdPosition::adItemType($ad_position->type));
        $this->assign('ad_position',$ad_position);
        $this->assign('ad_item',$ad_item);
        return $this->fetch();
    }

    private function updateAdItem($ad_item){
        // 表单 验证


        $data   =   input('post.');
        $data['start_time']     =   empty($data['start_time'])?time():strtotime($data['start_time']);
        $data['end_time']       =   empty($data['end_time'])?strtotime('+1 month'):strtotime($data['end_time']);
        if ($ad_item->allowField(true)->isUpdate()->save($data)){
            $this->success('更新成功！',url('ad/aditem',['ad_position_id'=>$ad_item->ad_position_id]));
        }
        $this->error('更新失败！');

    }



}